package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzChargeOrder;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzStuRenewOrder;
import com.zzedu.dto.ZzChargeOrderDTO;
import com.zzedu.mapper.ZzChargeOrderMapper;
import com.zzedu.service.IZzChargeOrderService;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.service.IZzStuRenewOrderService;
import com.zzedu.vo.ZzChargeOrderVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 收费订单Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-10-15
 */
@Service
public class ZzChargeOrderServiceImpl extends ServiceImpl<ZzChargeOrderMapper, ZzChargeOrder> implements IZzChargeOrderService {

    @Autowired
    IZzSchoolService zzSchoolService;

    @Autowired
    IZzStuRenewOrderService zzStuRenewOrderService;

    @Override
    public LambdaQueryWrapper<ZzChargeOrder> buildQuery(ZzChargeOrderDTO zzChargeOrder) {
        LambdaQueryWrapper<ZzChargeOrder> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.isNotEmpty(zzChargeOrder.getSchoolIdArray())) {
            lambdaQueryWrapper.in(ZzChargeOrder::getSchoolId, zzChargeOrder.getSchoolIdArray());
        }
        lambdaQueryWrapper.eq(null != zzChargeOrder.getPayState(), ZzChargeOrder::getPayState, zzChargeOrder.getPayState());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzChargeOrder.getCalcMonth()), ZzChargeOrder::getCalcMonth, zzChargeOrder.getCalcMonth());

        lambdaQueryWrapper.orderByDesc(ZzChargeOrder::getSchoolId).orderByDesc(ZzChargeOrder::getRenewType).orderByDesc(ZzChargeOrder::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public List<ZzChargeOrderVO> convertVO(List<ZzChargeOrder> zzChargeOrderList) {
        List<ZzChargeOrderVO> list = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(zzChargeOrderList)) {
            List<ZzSchool> zzSchoolList = this.zzSchoolService.listByIds(zzChargeOrderList.stream().map(ZzChargeOrder::getSchoolId).collect(Collectors.toList()));
            Map<Long, ZzSchool> map = zzSchoolList.stream().collect(Collectors.toMap(ZzSchool::getId, Function.identity()));
            for (ZzChargeOrder zzChargeOrder : zzChargeOrderList) {
                ZzSchool zzSchool = map.get(zzChargeOrder.getSchoolId());
                ZzChargeOrderVO zzChargeOrderVO = new ZzChargeOrderVO();
                BeanUtils.copyBeanProp(zzChargeOrderVO, zzChargeOrder);
                if (null != zzSchool) {
                    zzChargeOrderVO.setSchoolName(zzSchool.getSchoolName());
                }
                list.add(zzChargeOrderVO);
            }
        }

        return list;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean confirm(List<Long> ids) {
        List<ZzChargeOrder> zzChargeOrderList = this.list(Wrappers.lambdaQuery(ZzChargeOrder.class)
                .in(ZzChargeOrder::getId, ids)
                .eq(ZzChargeOrder::getPayState, CommonEnums.NO.getCode()));
        for (ZzChargeOrder zzChargeOrder : zzChargeOrderList) {
            CommonEnums commonEnums = CommonEnums.getCode(zzChargeOrder.getPayState());
            if (commonEnums == CommonEnums.YES) {
                continue;
            }
            zzChargeOrder.setPayState(CommonEnums.YES.getCode());
            zzChargeOrder.setDoTime(LocalDateTime.now());
            zzChargeOrder.setDoUserId(SecurityUtils.getUserId());
            this.updateById(zzChargeOrder);
            zzSchoolService.update(Wrappers.lambdaUpdate(ZzSchool.class)
                    .eq(ZzSchool::getId, zzChargeOrder.getSchoolId())
                    .set(ZzSchool::getState, CommonEnums.YES.getCode())
                    .set(ZzSchool::getGmtModify, LocalDateTime.now())
                    .set(ZzSchool::getUpdateBy, SecurityUtils.getUserId()));

            zzStuRenewOrderService.update(Wrappers.lambdaUpdate(ZzStuRenewOrder.class)
                    .eq(ZzStuRenewOrder::getPayChargeId, zzChargeOrder.getId())
                    .ne(ZzStuRenewOrder::getValidFlag, 4)
                    .set(ZzStuRenewOrder::getPayState, CommonEnums.YES.getCode())
                    .set(ZzStuRenewOrder::getGmtModify, LocalDateTime.now())
                    .set(ZzStuRenewOrder::getUpdateBy, SecurityUtils.getUserId())
            );
            zzSchoolService.refreshCache(zzChargeOrder.getSchoolId());
        }
        return true;
    }
}